package cn.ffcs.uec.staff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.staff.component.bean.UecBmDataListboxBean;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.manager.UecBmDataManager;
import cn.ffcs.uec.staff.model.UecBmData;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;
/**
 * @Title:考核信息 CRUD插件
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2017年11月27日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecBmDataListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecBmDataListboxBean bean = new UecBmDataListboxBean();
	
	private UecBmData uecBmData;

	private UecBmDataManager uecBmDataManager = (UecBmDataManager) ApplicationContextUtil
			.getBean("uecBmDataManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecBmDataListboxExt() {
		Executions.createComponents(UecStaffConstants.UEC_BM_DATA_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecStaffConstants.ON_UEC_BM_DATA_QUERY, this,
				UecStaffConstants.ON_UEC_BM_DATA_QUERY_RESPONSE);

		this.addForward(UecStaffConstants.ON_CLEAN_UEC_BM_DATA, this,
				UecStaffConstants.ON_CLEAN_UEC_BM_DATA_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecBmDataList() throws Exception {
		if (this.bean.getUecBmDataListbox().getSelectedCount() > 0) {
			uecBmData = (UecBmData) bean.getUecBmDataListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecBmDataQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (null != uecStaffInfo) {
			uecBmData = new UecBmData();
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
					&& !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
				uecBmData.setPkPsndoc(uecStaffInfo.getPkPsndoc());
				this.setButtonValid(true, false, false,false);
			} else {
				uecBmData.setStaffId(uecStaffInfo.getStaffId());
				this.setButtonValid(true, true,true,true);
			}
			this.onQueryUecBmData();
		}
	}

	public void onCleanUecBmDataResponse(ForwardEvent event) {
		clearVariable();
		uecStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecBmDataListbox());
		this.bean.getUecBmDataListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecBmData() {
		this.bean.getUecBmDataListboxPaging().setActivePage(0);
		this.onQueryUecBmDataList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecBmData() {
		ListboxUtils.clearListbox(this.bean.getUecBmDataListbox());
		this.bean.getUecBmDataListboxPaging().setTotalSize(1);
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecBmData() throws Exception, InterruptedException {
		if (!StrUtil.isNullOrEmpty(uecBmData)
				&& !StrUtil.isNullOrEmpty(uecBmData.getId())) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecBmData", uecBmData);
			Window window = (Window) Executions.createComponents(
					UecStaffConstants.UEC_BM_DATA_EDIT_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecBmData() throws Exception {
		if (null != uecStaffInfo) {
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
					&& !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
				ZkUtil.showError("集团下发数据，禁止操作！", "温馨提示");
			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("opType", "add");
				map.put("uecStaffInfo", uecStaffInfo);
				Window window = (Window) Executions.createComponents(
						UecStaffConstants.UEC_BM_DATA_EDIT_ZUL, this, map);
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecBmData = (UecBmData) event.getData();
							onQueryUecBmDataList();
						}
					}
				});
			}
		}
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecBmData() throws Exception, InterruptedException {
		if (null != uecBmData && null != uecBmData.getId()) {
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
					&& !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
				ZkUtil.showError("集团下发数据，禁止操作！", "温馨提示");
			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("opType", "edit");
				map.put("uecBmData", uecBmData);
				Window window = (Window) Executions.createComponents(
						UecStaffConstants.UEC_BM_DATA_EDIT_ZUL, this, map);
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecBmData = (UecBmData) event.getData();
							onQueryUecBmDataList();
						}
					}
				});
			}
		} else {
			ZkUtil.showError("请选择要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecBmData() {
		if (this.uecBmData != null && uecBmData.getId() != null) {
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
					&& !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
				ZkUtil.showError("集团下发数据，禁止操作！", "温馨提示");
			} else {
				ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
					public void onEvent(Event event) throws Exception {
						Integer result = (Integer) event.getData();
						if (result == Messagebox.OK) {
							uecBmData.removeOnly();
							PubUtil.reDisplayListbox(bean.getUecBmDataListbox(),
									uecBmData, "del");
							uecBmData = null;
						}
					}
				});
			}
		} else {
			ZkUtil.showError("请选择要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecBmDataListboxPaging() {
		this.onQueryUecBmDataList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecBmData = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecBmDataList() {
		if (null != uecBmData) {
			PageInfo pageInfo = uecBmDataManager.queryPageInfoByUecBmData(uecBmData,
					this.bean.getUecBmDataListboxPaging().getActivePage() + 1,
					this.bean.getUecBmDataListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecBmDataListbox().setModel(list);
			this.bean.getUecBmDataListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}

}
